package com.gmcc.docmanager.control;

import android.content.Context;

import com.gmcc.docmanager.app.Constants;
import com.gmcc.docmanager.entity.LibCodeType;
import com.gmcc.docmanager.loader.DocDetailResponseParser;
import com.gmcc.docmanager.loader.DocSearchResponseParser;
import com.gmcc.docmanager.loader.Request;
import com.gmcc.docmanager.loader.Response;
import com.gmcc.docmanager.loader.ResponseListener;
import com.gmcc.docmanager.loader.Sender;

public class DocumentControl extends AbstractControl{

	private static DocumentControl instance;

	private Context mContext;

	private DocumentControl(Context context) {
		mContext = context;
	}

	public static void init(Context context) {
		instance = new DocumentControl(context);
	}

	public static DocumentControl getInstance() {
		return instance;
	}

	public void searchDocumentList(LibCodeType libCodeType, String keywords,
			int startNum, int pageNum, final SearchDocumentListListener listener) {

		if (!LoginControl.isLogin()) {
			listener.onResponseFail(new SearchDocumentListResponse(
					createNotLoginResponse()));
			return;
		}

		Request request = new Request();
		if (Constants.IS_INTERFACE_TEST) {
			request.setRequestUrl(Constants.SEARCH_URL_TEST);
		} else {
			request.setRequestUrl(Constants.SEARCH_URL);
		}
		request.addRequestData("libCode", String.valueOf(libCodeType.value()));
		request.addRequestData("keywords", keywords);
		request.addRequestData("bpos", String.valueOf(startNum));
		request.addRequestData("pageNum", String.valueOf(pageNum));

		DocSearchResponseParser responseParser = new DocSearchResponseParser(
				new ResponseListener() {
					@Override
					public void onResponse(Response response) {
						if (response.getCode() != 0) {
							listener.onResponseFail(new SearchDocumentListResponse(
									response));
						} else {
							listener.onResponseSuccess(new SearchDocumentListResponse(
									response));
						}
					}
				});

		Sender sender = new Sender(mContext, request, responseParser);
		sender.send();
	}

	public interface SearchDocumentListListener {
		void onResponseSuccess(SearchDocumentListResponse response);

		void onResponseFail(SearchDocumentListResponse response);
	}

	public void getDocumentDetail(LibCodeType libCodeType, String docID,
			final DocumentDetailListener listener) {
		if (!LoginControl.isLogin()) {
			listener.onResponseFail(new DocumentDetailResponse(
					createNotLoginResponse()));
			return;
		}

		Request request = new Request();
		if (Constants.IS_INTERFACE_TEST) {
			request.setRequestUrl(Constants.GET_DETAIL_URL_TEST);
		} else {
			request.setRequestUrl(Constants.GET_DETAIL_URL + docID);
		}
		request.addRequestData("libCode", String.valueOf(libCodeType.value()));
		request.addRequestData("docID", docID);

		DocDetailResponseParser responseParser = new DocDetailResponseParser(
				new ResponseListener() {
					@Override
					public void onResponse(Response response) {
						if (response.getCode() != 0) {
							listener.onResponseFail(new DocumentDetailResponse(
									response));
						} else {
							listener.onResponseSuccess(new DocumentDetailResponse(
									response));
						}
					}
				});

		Sender sender = new Sender(mContext, request, responseParser);
		sender.send();
	}

	public interface DocumentDetailListener {
		void onResponseSuccess(DocumentDetailResponse response);

		void onResponseFail(DocumentDetailResponse response);
	}
}
